﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms
{
    public partial class FR_Add_Hospital : DevExpress.XtraEditors.XtraForm
    {
        string ma_benh_vien = "";
        string new_id = "";
        bool isUpdate = false;
        IDIC_HOSPITALData _IDIC_HOSPITALData;
        IDIC_PROVINCEData _IDIC_PROVINCEData;

        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_Add_Hospital(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_Add_Hospital(bool _isUpdate, string mabv)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            ma_benh_vien = mabv;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IDIC_PROVINCEData = (IDIC_PROVINCEData)Activator.GetObject(typeof(IDIC_PROVINCEData), util.ChuoiDIC_PROVINCEData);
            _IDIC_HOSPITALData = (IDIC_HOSPITALData)Activator.GetObject(typeof(IDIC_HOSPITALData), util.Chuoibv);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            
            tangid();
            
            load_ds_tinh();

            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id;
            }
        }

        private void load_ds_tinh()
        {
            lu_tinh.Properties.DataSource = _IDIC_PROVINCEData.DIC_PROVINCE_GetList().Tables[0];
            lu_tinh.Properties.DisplayMember = "ProvinceName";
            lu_tinh.Properties.ValueMember = "ProvinceCode";
        }

        private void tangid()
        {
            new_id = _icommon.Generate_ID("DIC_HOSPITAL", "HospitalCode", "BV");
        }

        private void loadthongtin()
        {
            DIC_HOSPITAL bv = Laythongtin(ma_benh_vien);
            
            txtma.Text = bv.HospitalCode;
            txtten.Text = bv.HospitalName;
            txtghichu.Text = bv.Description;
            lu_tinh.EditValue = bv.ProvinceCode;
            chkconquanly.Checked = bv.Active;
        }

        private DIC_HOSPITAL Laythongtin(string HospitalCode)
        {
            DataSet ds = _IDIC_HOSPITALData.DIC_HOSPITAL_Get(HospitalCode);

            DIC_HOSPITAL bv = new DIC_HOSPITAL();

            if (ds.Tables.Count>0)
            {
                string ma = ds.Tables[0].Rows[0][0].ToString();
                string matinh = ds.Tables[0].Rows[0][1].ToString();
                string ten = ds.Tables[0].Rows[0][2].ToString();
                string ghichu = ds.Tables[0].Rows[0][3].ToString();
                string active = ds.Tables[0].Rows[0][4].ToString();

                bv.Active = bool.Parse(active);
                bv.Description = ghichu;
                bv.HospitalCode = ma;
                bv.HospitalName = ten;
                bv.ProvinceCode = matinh;
            }

            return bv;
        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            bool active = chkconquanly.Checked;
            string matinh = util.Get_select_value_lookupedit(lu_tinh, "ProvinceCode");

            int kq = _IDIC_HOSPITALData.DIC_HOSPITAL_Insert(ma, matinh, ten, ghichu, active);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            bool active = chkconquanly.Checked;
            string matinh = util.Get_select_value_lookupedit(lu_tinh, "ProvinceCode");
            return _IDIC_HOSPITALData.DIC_HOSPITAL_Update(ma, matinh, ten, ghichu, active);
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtghichu.Text = "";
            txtma.Text = new_id;
            txtten.Text = "";
            lu_tinh.EditValue = null;
            chkconquanly.Checked = true;
            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}